package com.lushu.chapitre1.graphics.elements {
import com.sfdk.utils.IntPoint;

    import flash.events.Event;

    public class MovableElement extends InteractiveElement {
    public var team:int;

    private var _isMoving:Boolean;
    private var _movementPath:Vector.<IntPoint>;
    private var _movementIndex:int;
    private var _currentTargetX:int;
    private var _currentTargetY:int;
    private var _remainingX:Number;
    private var _remainingY:Number;
    private var _currentMoveFrame:Number;

    private const _orthogonalSpeed:int = 1;
    private const _diagonalSpeed:Number = Math.SQRT1_2;
    private const _slowStartFrames:Number = 1;

    protected var _speed:Number = 2;

    // ITurnBasedElement
    protected var _turnStartPos : IntPoint;
    protected var _moveStat : int = 6;
    private var _movementInitX:int;
    private var _movementInitY:int;
    private var _movementDestX:int;
    private var _movementDestY:int;


    public function MovableElement(animatedBitmapType:String) {
        super(animatedBitmapType);
    }

    public function get turnStartPos():IntPoint {
        return _turnStartPos;
    }

    public function set turnStartPos(value:IntPoint):void {
        _turnStartPos = value;
    }

    public function get moveStat():int {
        return _moveStat;
    }

    public function set moveStat(value:int):void {
        _moveStat = value;
    }

    public function saveTurnState() : void
    {
        _turnStartPos = new IntPoint(x / (24), y / (24));
    }

    public function setDirection(xDiff : int, yDiff : int) : void
    {
        if(xDiff > 0)
            currentFace = "Droite";
        else if(xDiff < 0)
            currentFace = "Gauche";
        else if(yDiff < 0)
            currentFace = "Dos";
        else if(yDiff > 0)
            currentFace = "Face";
    }

    public function setFacing(direction : String):void {
        currentFace = direction;
    }

    public function getDirection() : String
    {
        return _currentFace;
    }

    public function moveTo(path:Vector.<IntPoint>):void {
        _movementPath = path;
        _movementIndex = 0;
        if (!_isMoving) {
            _isMoving = true;
            _currentMoveFrame = 0;
            _currentTargetX = (_movementPath[0].x * 24);
            _currentTargetY = (_movementPath[0].y * 24);

            _movementInitX = int(_x / 24);
            _movementInitY = int(_y / 24);
            _movementDestX = _movementPath[0].x;
            _movementDestY = _movementPath[0].y;
        }
    }

    public function get isMoving():Boolean {
        return _isMoving;
    }




    override public function update(inMenu : Boolean):Boolean {
        if (super.update(inMenu)) {
            if(!inMenu)
                updateMovement();
            //updateAnimation();
            return true;
        }
        else
            return false;
    }

    private function updateMovement():void {
        if (_isMoving) {
            currentAction = "Marche";
            //var slowStartSpeed:Number = _currentMoveFrame / _slowStartFrames;
            //if (slowStartSpeed > 1) slowStartSpeed = 1;
            var moveSpeed:Number;

            _remainingX = _currentTargetX - _x;
            _remainingY = _currentTargetY - _y;
            var remainingXAbs:Number = _remainingX;
            if (remainingXAbs < 0)
                remainingXAbs = -_remainingX;
            var remainingYAbs:Number = _remainingY;
            if (remainingYAbs < 0)
                remainingYAbs = -_remainingY;

//            if (remainingXAbs > 0 && remainingYAbs > 0) { // Diagonal movement
//                moveSpeed = _diagonalSpeed * _speed * slowStartSpeed;
//                if (remainingXAbs < moveSpeed)
//                    _x += _remainingX;
//                else
//                    _x += (_remainingX / remainingXAbs) * moveSpeed;
//
//                if (_remainingX > 0)
//                    currentFace = "Droite";
//                else
//                    currentFace = "Gauche";
//
//                if (remainingYAbs < moveSpeed)
//                    _y += _remainingY;
//                else
//                    _y += (_remainingY / remainingYAbs) * moveSpeed;
//
//                hasMoved = true;
//            } else
            if (remainingXAbs > 0) { // Orthogonal movement along X
                if (_remainingX > 0)
                    currentFace = "Droite";
                else
                    currentFace = "Gauche";
                moveSpeed = _speed;
                if (remainingXAbs < moveSpeed)
                    _x += _remainingX;
                else
                    _x += (_remainingX / remainingXAbs) * moveSpeed;
                hasMoved = true;
            }
            else if (remainingYAbs > 0) { // Orthogonal movement along Y
                if (_remainingY > 0)
                    currentFace = "Face";
                else
                    currentFace = "Dos";
                moveSpeed = _speed;
                if (remainingYAbs < moveSpeed)
                    _y += _remainingY;
                else
                    _y += (_remainingY / remainingYAbs) * moveSpeed;

                hasMoved = true;
            }

            _remainingX = _currentTargetX - _x;
            _remainingY = _currentTargetY - _y;
            remainingXAbs = _remainingX;
            if (remainingXAbs < 0)
                remainingXAbs = -_remainingX;
            remainingYAbs = _remainingY;
            if (remainingYAbs < 0)
                remainingYAbs = -_remainingY;

            if(remainingXAbs == 0 && remainingYAbs == 0) {
                _movementIndex++;
            }

            if (_movementIndex >= _movementPath.length) {
                _isMoving = false;
                _movementInitX = _movementDestX;
                _movementInitY = _movementDestY;
                dispatchEvent(new Event("movementDone"));
                trace("Movement frame count for " + this.id + " = " + _currentMoveFrame);
            }
            else {
                _currentTargetX = (_movementPath[_movementIndex].x * 24);
                _currentTargetY = (_movementPath[_movementIndex].y * 24);
            }

            _currentMoveFrame++;
        }
        else {
            _currentAction = "Marche";
        }
    }

    public function get speed():Number {
        return _speed;
    }


    public function get movementInitX():int {
        return _movementInitX;
    }

    public function get movementInitY():int {
        return _movementInitY;
    }

    public function get movementDestX():int {
        return _movementDestX;
    }

    public function get movementDestY():int {
        return _movementDestY;
    }
}
}